import { ApiCall } from "tsrpc";
import { ReqExitSubWorld, ResExitSubWorld } from "../../shared/protocols/worldServer/PtlExitSubWorld";
import { WorldServerConn } from "../WorldServer";
import { worldServer } from "../../WorldServerMain";

export async function ApiExitSubWorld(call: ApiCall<ReqExitSubWorld, ResExitSubWorld>) {
    const conn = call.conn as WorldServerConn;

    if (conn.currentUser) {
        delete worldServer.userId2Conn[conn.currentUser?.uid];
    }

    if (conn.currentSubWorld) {
        conn.currentSubWorld.onRPC_Leave(conn);
    }

    call.succ({});
    setTimeout(() => {
        conn.close('USER_LEAVE');
    }, 100);
}